﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RTS_RPG
{
    public class PauseMenu
    {
        public Overworld worldref;
        public PlayerStats pstats;

        private enum Tab
        {
            Party,
            Equipment,
            Items,
            Data
        }

        private Vector2 mouse_pos;
        private bool scroll_over;
        private int current_selection;
        private Tab current_tab;
        private bool up_pressed;
        private bool down_pressed;
        private bool pow_pressed;
        private bool def_pressed;
        private bool spd_pressed;
        private bool int_pressed;
        private Vector2 resume_button_location;

        public PauseMenu(Overworld _worldref, PlayerStats _pstats)
        {
            worldref = _worldref;
            pstats = _pstats;
            mouse_pos = new Vector2(Program.NaN, Program.NaN);
            scroll_over = false;
            resume_button_location = new Vector2(Program.SCREENWIDTH * 2 / 3, 100);
            current_selection = 0;
            current_tab = Tab.Party;
            up_pressed = false;
            down_pressed = false;
            pow_pressed = false;
            def_pressed = false;
            spd_pressed = false;
            int_pressed = false;
        }

        public void draw(GameTime gameTime, GraphicsDevice graphics_dev, SpriteBatch spBatch)
        {
            graphics_dev.Clear(Color.Pink);
            if (worldref.in_battle) draw_battle_pause(gameTime, graphics_dev, spBatch);
            else draw_overworld_pause(gameTime, graphics_dev, spBatch);
        }

        public void update(GameTime gameTime, MouseState mstate, KeyboardState kstate)
        {
            mouse_pos = new Vector2(mstate.X - 16, mstate.Y - 16);
            if (kstate.IsKeyDown(Keys.Q)) 
                quit_pause_menu();
            else if (mstate.X >= resume_button_location.X   &&
                mstate.X <= resume_button_location.X + 130  &&
                mstate.Y >= resume_button_location.Y        &&
                mstate.Y <= resume_button_location.Y + 50)
            {
                if (mstate.LeftButton == ButtonState.Pressed) quit_pause_menu();
                scroll_over = true;
            }
            else scroll_over = false;


            // Move select cursor
            if (kstate.IsKeyDown(Keys.Up))
            {
                if (!up_pressed)
                {
                    --current_selection;
                    if (current_selection < 0) 
                        current_selection = pstats.party.Count;
                    up_pressed = true;
                }
            }
            else
                up_pressed = false;

            if (kstate.IsKeyDown(Keys.Down))
            {
                if (!down_pressed)
                {
                    ++current_selection;
                    if (current_selection > pstats.party.Count)
                        current_selection = 0;
                    down_pressed = true;
                }
            }
            else
                down_pressed = false;

            // Check if user is messin' with character stats
            apply_user_management_input(kstate);
        }

        private void draw_overworld_pause(GameTime gameTime, GraphicsDevice graphics_dev, SpriteBatch spBatch)
        {
            int i = 0;
            int x = 25;
            int y = Program.SCREENHEIGHT / 3;
            SpriteFont font = worldref.textures.font;
            foreach (Character member in pstats.party)
            {
                spBatch.Draw(member.avatar, new Vector2(x, y - 25), Color.White);
                if (current_selection == i + 1)
                    spBatch.Draw(worldref.textures.select_arrow, new Vector2(x, y - 25), Color.White);
                draw_character_stats(member, spBatch, font, x, y);
                y += 100;
                ++i;
                if (y >= Program.SCREENHEIGHT - 64)
                {
                    y = Program.SCREENHEIGHT / 3;
                    x += 350;
                }
            }
            draw_resume(spBatch, font);
            spBatch.Draw(worldref.textures.mouse, mouse_pos, Color.White);
        }

        private void draw_battle_pause(GameTime gameTime, GraphicsDevice graphics_dev, SpriteBatch spBatch)
        {
            int i = 0;
            int x = 25;
            int y = Program.SCREENHEIGHT / 3;
            SpriteFont font = worldref.currentBattle.textures.font;
            Battle_Unit unit;
            foreach (Character member in pstats.party)
            {
                unit = worldref.currentBattle.friendly[i];
                spBatch.Draw(unit.avatar, new Vector2(x, y - 25), Color.White);
                if (current_selection == i + 1)
                    spBatch.Draw(worldref.textures.select_arrow, new Vector2(x, y - 25), Color.White);
                draw_character_stats(member, spBatch, font, x, y);
                y += 100;
                if (y >= Program.SCREENHEIGHT - 64)
                {
                    y = Program.SCREENHEIGHT / 3;
                    x += 350;
                }
                ++i;
            }
            draw_resume(spBatch, font);
            spBatch.Draw(worldref.currentBattle.textures.mouse, mouse_pos, Color.White);
        }

        private void draw_character_stats(Character member, SpriteBatch spBatch, SpriteFont font, int x, int y)
        {
            spBatch.DrawString(font, "HP: " + member.hp.X + "/" + member.hp.Y,
                new Vector2(x + 80, y - 25), Color.Black);
            spBatch.DrawString(font, "MP: " + member.mp.X + "/" + member.mp.Y,
                new Vector2(x + 80, y - 5), Color.Black);
            spBatch.DrawString(font, "XP: " + member.xp.X + "/" + member.xp.Y,
                new Vector2(x + 80, y + 15), Color.Black);
            spBatch.DrawString(font, "  POW: " + member.stats.X,
                new Vector2(x + 180, y - 25), Color.Black);
            spBatch.DrawString(font, "  SPD: " + member.stats.Z,
                new Vector2(x + 250, y - 25), Color.Black);
            spBatch.DrawString(font, "  DEF: " + member.stats.Y,
                new Vector2(x + 180, y - 5), Color.Black);
            spBatch.DrawString(font, "  INT: " + member.stats.W,
                new Vector2(x + 250, y - 5), Color.Black);
            spBatch.DrawString(font, "  FP:  " + member.freePoints,
                new Vector2(x + 180, y + 15), Color.Black);
            spBatch.DrawString(font, "  LV:  " + member.level,
                new Vector2(x + 250, y + 15), Color.Black);
        }

        private void draw_resume(SpriteBatch spBatch, SpriteFont font)
        {
            if (scroll_over)
                spBatch.DrawString(font, "RESUME GAME", resume_button_location, Color.Green);
            else
                spBatch.DrawString(font, "RESUME GAME", resume_button_location, Color.Black);
        }

        private void quit_pause_menu()
        {
            worldref.pause_screen = false;
            current_selection = 0;
            worldref.textures.UnloadPauseMenuContent();
        }

        private void apply_user_management_input(KeyboardState kstate)
        {
            if (current_selection <= 0 || current_selection > pstats.party.Count)
                return;
            Character member = pstats.party[current_selection - 1];
            if (member.freePoints == 0) return;
            if (kstate.IsKeyDown(Keys.P))
            {
                if (!pow_pressed)
                {
                    --member.freePoints;
                    ++member.stats.X;
                    pow_pressed = true;
                }
            }
            else pow_pressed = false;
            if (kstate.IsKeyDown(Keys.D))
            {
                if (!def_pressed)
                {
                    --member.freePoints;
                    ++member.stats.Y;
                    def_pressed = true;
                }
            }
            else def_pressed = false;
            if (kstate.IsKeyDown(Keys.S))
            {
                if (!spd_pressed)
                {
                    --member.freePoints;
                    ++member.stats.Z;
                    spd_pressed = true;
                }
            }
            else spd_pressed = false;
            if (kstate.IsKeyDown(Keys.I))
            {
                if (!int_pressed)
                {
                    --member.freePoints;
                    ++member.stats.W;
                    int_pressed = true;
                }
            }
            else int_pressed = false;
        }
    }
}
